<?php
namespace Home\Controller;
use Think\Controller;
/*
  type=0 感染性
  type=1 损伤性
  type=3 化学性
  type=4 药物性
  type=5 病理性 
*/
class ReportController extends ZMController {
	public function report(){//某个时间段医院每天的收集情况
		$hid=I('hid');
		$startdate=I('startdate');
		$enddate=I('enddate');
		$region=session('user.region');
		$day=getDateFromRange($startdate,$enddate);
		$a=array();
		foreach($day as $v){
			$arr=$this->getCollect($v,$v,$hid,$region);
			foreach($arr as $ap){
				$ap['pdate']=$v;
				$a[]=$ap;
			}
		}
		$this->ajax($a);
	}
    public function reportDay(){//日报表
		$hid=I('hid');
		$startdate=I('date');
		$enddate=$startdate;
		$region=session('user.region');
		$arr=$this->getCollect($startdate,$enddate,$hid,$region);
		$this->ajax($arr);
	}
    public function reportMonth(){//月报表
		$hid=I('hid');
		$month=I('month');
		$year=date('Y',strtotime($month));
		$month=date('m',strtotime($month));
		$day=checkdays($year,$month);
		$startdate=$year.'-'.$month.'-01';
		$enddate=$year.'-'.$month.'-'.$day;
		$region=session('user.region');
		if($year<'2020'){
			$arr=$this->getCollect($startdate,$enddate,$hid,$region);
		}
		else{
			$arr=$this->getCollectOver($startdate,$enddate,$hid,$region);
			$now=date('Y-m-d');
			$y=$year.'-'.$month;//
			$ym=date('Y-m');
			if($y==$ym){//当月
				$collectnow=$this->getCollect($now,$now,$hid,$region);
				foreach($arr as &$v){
					foreach($collectnow as $vn){
						if($v['hid']==$vn['hid']){
							$v['nval_2']=bcadd($v['nval_2'],$vn['nval_2'],2);
							$v['nval_5']=bcadd($v['nval_5'],$vn['nval_5'],2);
							$v['totalweight']=bcadd($v['totalweight'],$vn['totalweight'],2);
							$v['weight_0']=bcadd($v['weight_0'],$vn['weight_0'],2);
							$v['weight_1']=bcadd($v['weight_1'],$vn['weight_1'],2);
							$v['weight_3']=bcadd($v['weight_3'],$vn['weight_3'],2);
							$v['weight_4']=bcadd($v['weight_4'],$vn['weight_4'],2);
							$v['weight_5']=bcadd($v['weight_5'],$vn['weight_5'],2);
							$v['weight_6']=bcadd($v['weight_6'],$vn['weight_6'],2);
							$v['weight_7']=bcadd($v['weight_7'],$vn['weight_7'],2);
							break;
						}
					}
				}
			}
		}
		$this->ajax($arr);
	}
	public function reportQuater(){//季度报表
		$date=I('quarter');
		$year=I('date');
		$region=session('user.region');
		switch($date){
			case 1:
			    $startdate=$year.'-01-01';
				$enddate=$year.'-03-31';
				break;
			case 2:
			    $startdate=$year.'-04-01';
				$enddate=$year.'-06-30';
				break;
			case 3:
			    $startdate=$year.'-07-01';
				$enddate=$year.'-09-30';
				break;
			case 4:
			    $startdate=$year.'-10-01';
				$enddate=$year.'-12-31';
				break;
		}
		$hid=I('hid');
		if($year<'2020'){
			$arr=$this->getCollect($startdate,$enddate,$hid,$region);
		}
		else{
			$arr=$this->getCollectOver($startdate,$enddate,$hid,$region);
			$now=date('Y-m-d');
			if($now>=$startdate&&$now<=$enddate){//
				$collectnow=$this->getCollect($now,$now,$hid,$region);
				foreach($arr as &$v){
					foreach($collectnow as $vn){
						if($v['hid']==$vn['hid']){
							$v['nval_2']=bcadd($v['nval_2'],$vn['nval_2'],2);
							$v['nval_5']=bcadd($v['nval_5'],$vn['nval_5'],2);
							$v['totalweight']=bcadd($v['totalweight'],$vn['totalweight'],2);
							$v['weight_0']=bcadd($v['weight_0'],$vn['weight_0'],2);
							$v['weight_1']=bcadd($v['weight_1'],$vn['weight_1'],2);
							$v['weight_3']=bcadd($v['weight_3'],$vn['weight_3'],2);
							$v['weight_4']=bcadd($v['weight_4'],$vn['weight_4'],2);
							$v['weight_5']=bcadd($v['weight_5'],$vn['weight_5'],2);
							$v['weight_6']=bcadd($v['weight_6'],$vn['weight_6'],2);
							$v['weight_7']=bcadd($v['weight_7'],$vn['weight_7'],2);
							break;
						}
					}
				}
			}
		}
		$this->ajax($arr);
	}
	public function reportYear(){//年度报表
		$year=I('date');
		$startdate=$year.'-01-01';
		$enddate=$year.'-12-31';
		$hid=I('hid');
		$region=session('user.region');
		if($year<'2020'){
			$arr=$this->getCollect($startdate,$enddate,$hid,$region);
		}
		else{
			$arr=$this->getCollectOver($startdate,$enddate,$hid,$region);
			$now=date('Y-m-d');
			$collectnow=$this->getCollect($now,$now,$hid,$region);
			foreach($arr as &$v){
				foreach($collectnow as $vn){
					if($v['hid']==$vn['hid']){
						$v['nval_2']=bcadd($v['nval_2'],$vn['nval_2'],2);
						$v['nval_5']=bcadd($v['nval_5'],$vn['nval_5'],2);
						$v['totalweight']=bcadd($v['totalweight'],$vn['totalweight'],2);
						$v['weight_0']=bcadd($v['weight_0'],$vn['weight_0'],2);
						$v['weight_1']=bcadd($v['weight_1'],$vn['weight_1'],2);
						$v['weight_3']=bcadd($v['weight_3'],$vn['weight_3'],2);
						$v['weight_4']=bcadd($v['weight_4'],$vn['weight_4'],2);
						$v['weight_5']=bcadd($v['weight_5'],$vn['weight_5'],2);
						$v['weight_6']=bcadd($v['weight_6'],$vn['weight_6'],2);
						$v['weight_7']=bcadd($v['weight_7'],$vn['weight_7'],2);
					}
				}
			}
		}
		$this->ajax($arr);
	}
	public function getCollect($startdate,$enddate,$hid=NULL,$region=NULL){//收集总量
		$Model=M()->db(1,'DB_CONFIG1');
		$type=array(0,1,3,4,5,6,7);
		if(!empty($hid)){//单个医院查询
			$r=R('Cust/hospital',array($hid));
		}
		else{//所有医院查询
			$r=R('Cust/hospital',array($hid,$region));
		}
		$m=array();
		foreach($r as $v){
			$hid=$v['hid'];
			$name=$v['name'];//医院名称
			$res=$Model->query("select cid,nval1_2,nval1_5 from ddwk_collect where pdate>='$startdate' and pdate<='$enddate' and hid=$hid");
			$cid='';
			$nval1=0;
			$nval2=0;
			$fval=0;
			foreach($res as $row){
				$cid.=','.$row['cid'];
				$nval1+=$row['nval1_2'];//利器盒
				$nval2+=$row['nval1_5'];//胎盘
			}
			$cid=ltrim($cid,',');
			foreach($type as $v){
				$arr['weight_'.$v]=R('Collect/totalCweight',array($cid,$v));
				$fval+=$arr['weight_'.$v];
			}
			$arr['hid']=$hid;
			$arr['nval_2']=$nval1;
			$arr['nval_5']=$nval2;
			$arr['totalweight']=$fval;
			$arr['hospname']=$name;
			$m[]=$arr;
		}
		return $m;
	}
	public function getCollectOver($startdate,$enddate,$hid=NULL,$region=NULL){//collectover表内的收集总量2020年开始
		$m=dateMonths($startdate,$enddate);//期间段月份集合
		$m=implode(',',$m);
		$Collect=M()->db(1,'DB_CONFIG1');
		$where['pdate']=array('in',$m);
		if(empty($hid)){//
			$r=R('Cust/hospital',array($hid,$region));
			$hid=i_array_column($r,'hid');
			$hid=implode(',',$hid);
		}
		if(!empty($hid)){
			$where['hid']=array('in',$hid);
		}
		$res=$Collect->table("ddwk_collect_over")->where($where)->select();
		$arr=array();
		foreach($res as $row){
			if(!isset($arr[$row['hid']])){
				$arr[$row['hid']] = $row;
			}
			else{
				$arr[$row['hid']]['weight0']+=$row['weight0'];
				$arr[$row['hid']]['weight1']+=$row['weight1'];
				$arr[$row['hid']]['weight3']+=$row['weight3'];
				$arr[$row['hid']]['weight4']+=$row['weight4'];
				$arr[$row['hid']]['weight5']+=$row['weight5'];
				$arr[$row['hid']]['weight6']+=$row['weight6'];
				$arr[$row['hid']]['weight7']+=$row['weight7'];
				$arr[$row['hid']]['nval1_2']+=$row['nval1_2'];
				$arr[$row['hid']]['nval1_5']+=$row['nval1_5'];
			}
		}
		$arr=array_values($arr);
		$m=array();
		foreach($arr as $v){
			unset($d);
			$hid=$v['hid'];
			$d['hid']=$hid;
			$d['hospname']=R('Cust/hospname',array($hid));
			$d['weight_0']=$v['weight0'];
			$d['weight_1']=$v['weight1'];
			$d['weight_3']=$v['weight3'];
			$d['weight_4']=$v['weight4'];
			$d['weight_5']=$v['weight5'];
			$d['weight_6']=$v['weight6'];
			$d['weight_7']=$v['weight7'];
			$d['nval_2']=$v['nval1_2'];
			$d['nval_5']=$v['nval1_5'];
			$d['totalweight']=$d['weight_0']+$d['weight_1']+$d['weight_3']+$d['weight_4']+$d['weight_5']+$d['weight_6']+$d['weight_7'];
			$m[]=$d;
		}
		return $m;
	}
}